package com.itheima.controller;

import com.itheima.MessageConstant;
import com.itheima.pojo.Picture;
import com.itheima.service.PictureService;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.PictureVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 图片相关接口
 * @author kehaonan
 */
@RestController
@RequestMapping("/picture")
@FeignClient
@Api(tags = "图片相关接口")
public class PictureController {

    @Autowired
    private PictureService pictureService;

    @ControllerMonitor
    @PostMapping("/add")
    @ApiOperation("添加图片信息")
    public String add(@RequestBody PictureVO pictureVO){
        try {
            pictureService.add(pictureVO);
            return ResultUtil.result(200, MessageConstant.PICTURE_ADD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.PICTURE_ADD_FAIL);
        }
    }

    @ControllerMonitor
    @PostMapping("/delete")
    @ApiOperation("删除图片信息")
    public String delete(@RequestBody PictureVO pictureVO){
        if (pictureVO.getUid()==null || "".equals(pictureVO.getUid())){
            return ResultUtil.result(401, MessageConstant.ADD_UID);
        }

        try {
            pictureService.delete(pictureVO);
            return ResultUtil.result(200, MessageConstant.PICTURE_DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.PICTURE_DELETE_FAIL);
        }
    }

//    @ControllerMonitor
    @PostMapping("/getList")
    @ApiOperation("获取图片信息列表")
    public String getList(@RequestBody PictureVO pictureVO){

        //设置默认当前页
        if (pictureVO.getCurrentPage()==null || "".equals(pictureVO.getCurrentPage())){
            pictureVO.setCurrentPage(0);
        }
        //设置默认每页条数
        if (pictureVO.getPageSize()==null || "".equals(pictureVO.getPageSize())){
            pictureVO.setPageSize(10);
        }

        try {
            List<Picture> list = pictureService.getList(pictureVO);
            return ResultUtil.result(200,list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.PICTURE_FINDALL_FAIL);
        }
    }

    @ControllerMonitor
    @PostMapping("/edit")
    @ApiOperation("修改图片信息")
    public String edit(@RequestBody PictureVO pictureVO){
        if (pictureVO.getUid()==null || "".equals(pictureVO.getUid())){
            return ResultUtil.result(401, MessageConstant.ADD_UID);
        }

        try {
            pictureService.edit(pictureVO);
            return ResultUtil.result(200, MessageConstant.PICTURE_EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.PICTURE_EDIT_FAIL);
        }
    }

    @ControllerMonitor
    @PostMapping("/setCover")
    @ApiOperation("通过图片uid将图片设置为封面")
    public String setCover(@RequestBody PictureVO pictureVO){
        if (pictureVO.getUid()==null || "".equals(pictureVO.getUid())){
            return ResultUtil.result(401, MessageConstant.ADD_UID);
        }
        try {
            pictureService.setCover(pictureVO);
            return ResultUtil.result(200, MessageConstant.PICTURE_COVER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.PICTURE_COVER_FAIL);
        }
    }

}
